package com.jeecms.wicool.dao.material.impl;

import java.util.List;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.material.WeixinMaterialDao;
import com.jeecms.wicool.entity.material.WeixinMaterial;

@Repository
public class WeixinMaterialDaoImpl extends HibernateBaseDao<WeixinMaterial, Integer>
		implements WeixinMaterialDao {

	@Override
	protected Class<WeixinMaterial> getEntityClass() {
		// TODO Auto-generated method stub
		return WeixinMaterial.class;
	}
	@Override
	public Pagination getPage(Integer shopId,Short messageType,int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from WeixinMaterial bean where 1=1 ");
			if (shopId!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",shopId);
			}
			if(messageType!=null){
				f.append(" and bean.messageType = :messageType");
				f.setParam("messageType",messageType);
			}if(shopId==null&&user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			f.append(" order by bean.id desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public WeixinMaterial findById(Integer id) {
		WeixinMaterial  entity = get(id);
		return entity;
	}
	@Override
	public WeixinMaterial save(WeixinMaterial bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public WeixinMaterial deleteById(Integer id) {
		WeixinMaterial entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	@Override
	public List<WeixinMaterial> getListWeixinMaterialByShopId(Integer shopId) {
		Finder f = Finder.create("select bean from WeixinMaterial bean where bean.shop.id = '"+ shopId+"'");
		return find(f);
	}
	
}